package com.terabits.smartriver.job;

import com.terabits.smartriver.mapper.DeviceMapper;
import com.terabits.smartriver.mapper.DeviceRunTimeMapper;
import com.terabits.smartriver.meta.po.Device;
import com.terabits.smartriver.meta.po.DeviceRunTime;
import com.terabits.smartriver.service.impl.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.terabits.smartriver.util.DateUtils.StringToDate;
import static com.terabits.smartriver.util.DateUtils.getTime;


@Component
public class runTimeTask extends BaseService {
    private static final String TYPE = "仪表";
    private static final int STATUS = 0;
    @Autowired
    private DeviceMapper deviceMapper;
    @Autowired
    private DeviceRunTimeMapper deviceRunTimeMapper;

    @Scheduled(cron = "0 0 1 ? * *")
//    @Scheduled(cron = "0 */2 * * * ?")
    public void CheckrunTimeTask() {

        //获取所有设备
        List<Device> devices = deviceMapper.selectAll();
        List<Device> useDevices = devices.stream().filter(device -> !TYPE.equals(device.getMotor()) && STATUS == (device.getIsWater())).collect(Collectors.toList());
        useDevices.stream().forEach(device -> {
            String boxInterface = device.getBoxInterface();
            if (!boxInterface.contains("k") || !boxInterface.contains("K")) {
                DeviceRunTime deviceRunTime1 = new DeviceRunTime();
                deviceRunTime1.setBoxNum(device.getBoxNum());
                deviceRunTime1.setBoxInterface(device.getBoxInterface());
                DeviceRunTime deviceRunTime2 = deviceRunTimeMapper.selectLimitOne(device.getBoxNum(), device.getBoxInterface());
                if (StringUtils.isEmpty(deviceRunTime2)) {
                    DeviceRunTime deviceRunTime = new DeviceRunTime();
                    deviceRunTime.setBoxNum(device.getBoxNum());
                    deviceRunTime.setBoxInterface(device.getBoxInterface());
                    deviceRunTime.setData(device.getValueLast());
                    deviceRunTime.setCountTime(Integer.valueOf(device.getRunTime()));
                    deviceRunTime.setRunTime(Integer.valueOf(device.getRunTime()));
                    deviceRunTime.setCreateTime(StringToDate(getTime(0).substring(0, 10), "yyyy-MM-dd"));
                    deviceRunTimeMapper.insert(deviceRunTime);
                } else {
                    //减去前一天的运行时间，统计当天的运行时间
                    Integer runTime = deviceRunTime2.getCountTime();
                    DeviceRunTime deviceRunTime = new DeviceRunTime();
                    deviceRunTime.setBoxNum(device.getBoxNum());
                    deviceRunTime.setBoxInterface(device.getBoxInterface());
                    deviceRunTime.setData(device.getValueLast());
                    deviceRunTime.setCountTime(Integer.valueOf(device.getRunTime()));
                    deviceRunTime.setRunTime((Integer.valueOf(device.getRunTime())) - runTime);
                    deviceRunTime.setCreateTime(StringToDate(getTime(0).substring(0, 10), "yyyy-MM-dd"));
                    deviceRunTimeMapper.insert(deviceRunTime);
                }


            }
        });
        System.out.println("##################################设备运行时间统计完成##################################");
    }
}
